package com.lc.demo.service.impl;

import com.lc.demo.bean.Course;
import com.lc.demo.bean.SignPerson;
import com.lc.demo.bean.Student;
import com.lc.demo.bean.Teacher;
import com.lc.demo.mapper.CourseMapper;
import com.lc.demo.mapper.TeacherMapper;
import com.lc.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Course> getCourseList(Course course) {
        return courseMapper.select(course);
    }

    @Transactional
    @Override
    public void addCourse(Course course) {
        Integer teacherid = course.getTeacherid();

        Teacher teacher=new Teacher();
        teacher.setId(teacherid);
        Teacher teacherAll = teacherMapper.selectOne(teacher);
        course.setTeachername(teacherAll.getName());
        courseMapper.insertSelective(course);
        List<Course> courses = courseMapper.selectAll();
        Course course1 = courses.get(courses.size() - 1);
        Integer courseId = course1.getId();
        //获取班级 插入关系表
        String[] split = course.getClassgradeids();
        for (int i = 0; i < split.length; i++) {
            courseMapper.insertGradeCourse(Integer.valueOf(split[i]), courseId);
        }

    }

    @Transactional
    @Override
    public void updateCourse(Course course) {
        //根据courseId删除 关系表中的数据 在插入
        Integer courseId = course.getId();

        courseMapper.delGradeCourse(courseId);
        Integer teacherid = course.getTeacherid();

        Teacher teacher=new Teacher();
        teacher.setId(teacherid);
        Teacher teacherAll = teacherMapper.selectOne(teacher);
        course.setTeachername(teacherAll.getName());
        //获取班级 插入关系表
         String[] split = course.getClassgradeids();
        for (int i = 0; i < split.length; i++) {
            courseMapper.insertGradeCourse(Integer.valueOf(split[i]), courseId);
        }
        courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public void delCourse(Course course) {
        courseMapper.delGradeCourse(course.getId());
        courseMapper.deleteSignin(course.getId());
        courseMapper.deleteLeavetable(course.getId());
        courseMapper.deleteByPrimaryKey(course.getId());
    }

    @Override
    public List<SignPerson> findSignPerson(Course coursequery) {
        List<Course> courseList = courseMapper.select(coursequery);
        List<SignPerson> signPersonList=new ArrayList<>();

        for (Course course : courseList) {
            //查询出 改课程签到的人数
            List<Student> studentSign = courseMapper.selectSignPerson(course.getId());
            //查询出未到的人数
            List<Student> studentNoSing = courseMapper.selectNoSignPerson(course.getId());
            //查询出哪些人没有签到
            String noSignPersonName="";
            StringBuilder stringBuilder=new StringBuilder();
            for (Student student : studentNoSing) {
                stringBuilder.append(student.getName()).append(",");
            }

            if (stringBuilder.length()>0){
                String s = stringBuilder.toString();
                noSignPersonName=s.substring(0,stringBuilder.toString().length()-1);
            }else {
                noSignPersonName="学生全部签到";
            }
            Course course1 = courseMapper.selectByPrimaryKey(course.getId());
            Integer total=studentSign.size()+studentNoSing.size();
            SignPerson signPerson=new SignPerson();
            signPerson.setAllPerson(total);
            signPerson.setName(course1.getName());
            signPerson.setNoSignPerson(studentNoSing.size());
            signPerson.setSignPerson(studentSign.size());
            signPerson.setStudentName(noSignPersonName);
            signPerson.setBeginTime(course1.getBegintime());
            signPerson.setEndTime(course1.getEndtime());
            signPerson.setWeekday(course1.getWeekday());
            signPersonList.add(signPerson);
        }
        return signPersonList;
    }




}
